home *** CD-ROM | disk | FTP | other *** search
/ Hardcore Visual Basic 5.0 (2nd Edition) / Hardcore Visual Basic 5.0 - Second Edition (1997)(Microsoft Press).iso / Code / TSORT.frm < prev    next >
Text File  |  1997-06-14  |  13KB  |  496 lines

  1. VERSION 5.00
  2. Object = "{2DD06898-E157-11D0-8C51-00C04FC29CEC}#1.1#0"; "ListBoxPlus.ocx"
  3. Begin VB.Form FTestSort 
  4.    Caption         =   "Test Sort"
  5.    ClientHeight    =   6600
  6.    ClientLeft      =   1215
  7.    ClientTop       =   2115
  8.    ClientWidth     =   5205
  9.    BeginProperty Font 
  10.       Name            =   "MS Sans Serif"
  11.       Size            =   8.25
  12.       Charset         =   0
  13.       Weight          =   700
  14.       Underline       =   0   'False
  15.       Italic          =   0   'False
  16.       Strikethrough   =   0   'False
  17.    EndProperty
  18.    Icon            =   "TSORT.frx":0000
  19.    LinkTopic       =   "Form1"
  20.    ScaleHeight     =   6600
  21.    ScaleWidth      =   5205
  22.    Begin ListBoxPlus.XListBoxPlus list 
  23.       Height          =   2565
  24.       Left            =   3465
  25.       TabIndex        =   17
  26.       Top             =   1980
  27.       Width           =   1470
  28.       _ExtentX        =   2593
  29.       _ExtentY        =   4233
  30.       BackColor       =   16777215
  31.       ListCount       =   0
  32.       ListIndex       =   -1
  33.       Completion      =   0   'False
  34.    End
  35.    Begin VB.ListBox lstSort 
  36.       Height          =   840
  37.       ItemData        =   "TSORT.frx":0CFA
  38.       Left            =   3447
  39.       List            =   "TSORT.frx":0D10
  40.       TabIndex        =   16
  41.       Top             =   420
  42.       Width           =   1488
  43.    End
  44.    Begin VB.CommandButton cmdReplaceSList 
  45.       Caption         =   "Replace..."
  46.       Height          =   375
  47.       Left            =   3465
  48.       TabIndex        =   15
  49.       Top             =   6108
  50.       Width           =   1416
  51.    End
  52.    Begin VB.CommandButton cmdFindSList 
  53.       Caption         =   "Find..."
  54.       Height          =   375
  55.       Left            =   3465
  56.       TabIndex        =   14
  57.       Top             =   4692
  58.       Width           =   1416
  59.    End
  60.    Begin VB.CommandButton cmdInsertSList 
  61.       Caption         =   "Insert..."
  62.       Height          =   375
  63.       Left            =   3465
  64.       TabIndex        =   13
  65.       Top             =   5172
  66.       Width           =   1416
  67.    End
  68.    Begin VB.CommandButton cmdRemoveSList 
  69.       Caption         =   "Remove..."
  70.       Height          =   375
  71.       Left            =   3465
  72.       TabIndex        =   12
  73.       Top             =   5652
  74.       Width           =   1416
  75.    End
  76.    Begin VB.TextBox txtArray 
  77.       Height          =   2568
  78.       Left            =   276
  79.       MultiLine       =   -1  'True
  80.       TabIndex        =   11
  81.       Top             =   1968
  82.       Width           =   1476
  83.    End
  84.    Begin VB.CommandButton cmdFindArray 
  85.       Caption         =   "Find..."
  86.       Height          =   375
  87.       Left            =   276
  88.       TabIndex        =   10
  89.       Top             =   4680
  90.       Width           =   1476
  91.    End
  92.    Begin VB.CommandButton cmdRemoveCollect 
  93.       Caption         =   "Remove..."
  94.       Height          =   375
  95.       Left            =   1872
  96.       TabIndex        =   9
  97.       Top             =   5652
  98.       Width           =   1464
  99.    End
  100.    Begin VB.CommandButton cmdInsertCollect 
  101.       Caption         =   "Insert..."
  102.       Height          =   375
  103.       Left            =   1872
  104.       TabIndex        =   8
  105.       Top             =   5172
  106.       Width           =   1464
  107.    End
  108.    Begin VB.CommandButton cmdFindCollect 
  109.       Caption         =   "Find..."
  110.       Height          =   375
  111.       Left            =   1872
  112.       TabIndex        =   7
  113.       Top             =   4692
  114.       Width           =   1464
  115.    End
  116.    Begin VB.TextBox txtCollect 
  117.       Height          =   2595
  118.       Left            =   1872
  119.       MultiLine       =   -1  'True
  120.       TabIndex        =   3
  121.       Top             =   1968
  122.       Width           =   1464
  123.    End
  124.    Begin VB.CommandButton cmdExit 
  125.       Cancel          =   -1  'True
  126.       Caption         =   "E&xit"
  127.       Height          =   375
  128.       Left            =   276
  129.       TabIndex        =   1
  130.       Top             =   6096
  131.       Width           =   1476
  132.    End
  133.    Begin VB.CheckBox chkDirection 
  134.       Caption         =   "High to Low"
  135.       Height          =   288
  136.       Left            =   3447
  137.       TabIndex        =   0
  138.       Top             =   72
  139.       Width           =   1416
  140.    End
  141.    Begin VB.Label lbl 
  142.       Caption         =   "Array"
  143.       Height          =   216
  144.       Index           =   2
  145.       Left            =   276
  146.       TabIndex        =   6
  147.       Top             =   1728
  148.       Width           =   1476
  149.    End
  150.    Begin VB.Label lbl 
  151.       Caption         =   "Collection"
  152.       Height          =   240
  153.       Index           =   1
  154.       Left            =   1872
  155.       TabIndex        =   5
  156.       Top             =   1728
  157.       Width           =   1464
  158.    End
  159.    Begin VB.Label lbl 
  160.       Caption         =   "List Box Plus"
  161.       Height          =   372
  162.       Index           =   0
  163.       Left            =   3465
  164.       TabIndex        =   4
  165.       Top             =   1728
  166.       Width           =   1416
  167.    End
  168.    Begin VB.Label lblOut 
  169.       Height          =   375
  170.       Left            =   276
  171.       TabIndex        =   2
  172.       Top             =   120
  173.       Width           =   3135
  174.    End
  175. End
  176. Attribute VB_Name = "FTestSort"
  177. Attribute VB_GlobalNameSpace = False
  178. Attribute VB_Creatable = False
  179. Attribute VB_PredeclaredId = True
  180. Attribute VB_Exposed = False
  181. Option Explicit
  182.  
  183. Private aInts(1 To 10) As Variant
  184. Private aStrs(1 To 10) As Variant
  185. Private aConst(1 To 10) As String
  186. Private nStrs As Collection
  187. Private esmlMode As ESortModeList
  188. Private helper As CSortHelper
  189.  
  190. Sub Form_Load()
  191.     
  192.     Set helper = New CSortHelper
  193.     Set nStrs = New Collection
  194.     
  195.     aConst(1) = "One"
  196.     aConst(2) = "two"
  197.     aConst(3) = "Three"
  198.     aConst(4) = "four"
  199.     aConst(5) = "Five"
  200.     aConst(6) = "six"
  201.     aConst(7) = "Seven"
  202.     aConst(8) = "Eight"
  203.     aConst(9) = "Nine"
  204.     aConst(10) = "ten"
  205.     
  206.     aInts(1) = 5
  207.     aInts(2) = 4
  208.     aInts(3) = 9
  209.     aInts(4) = 1
  210.     aInts(5) = 7
  211.     aInts(6) = 6
  212.     aInts(7) = 3
  213.     aInts(8) = 2
  214.     aInts(9) = 10
  215.     aInts(10) = 8
  216.     
  217.     aStrs(1) = "Five"
  218.     aStrs(2) = "four"
  219.     aStrs(3) = "Nine"
  220.     aStrs(4) = "One"
  221.     aStrs(5) = "Seven"
  222.     aStrs(6) = "six"
  223.     aStrs(7) = "Three"
  224.     aStrs(8) = "two"
  225.     aStrs(9) = "ten"
  226.     aStrs(10) = "Eight"
  227.     
  228.     nStrs.Add "Apple"
  229.     nStrs.Add "bean"
  230.     nStrs.Add "Pear"
  231.     nStrs.Add "banana"
  232.     nStrs.Add "peach"
  233.     nStrs.Add "CarRot"
  234.     nStrs.Add "appleberry"
  235.     nStrs.Add "Tangerine"
  236.     nStrs.Add "wine"
  237.     nStrs.Add "Beer"
  238.     
  239.     ' Put some items in a list box
  240.     list.Clear
  241.     list.Add "BEAR"
  242.     list.Add "Lion"
  243.     list.Add "tiger"
  244.     list.Add "dog"
  245.     list.Add "ZebrA"
  246.     list.Add "kangaroo"
  247.     list.Add "ELK"
  248.     list.Add "WartHog"
  249.     list.Add "Elephant"
  250.     list.Add "stoat"
  251.     
  252.     Show
  253.     
  254.     lstSort.ListIndex = 0
  255.    
  256.     SortAll
  257.     
  258. End Sub
  259.  
  260. Sub cmdExit_Click()
  261.     Unload Me
  262. End Sub
  263.  
  264. Private Sub cmdFindArray_Click()
  265.     Dim iPos As Long, vKey As Variant, f As Boolean
  266.     vKey = InputBox("Array item to find")
  267.     If esmlMode = esmSortVal Then
  268.         vKey = LookupString(aConst, vKey)
  269.         f = BSearchArray(aInts(), vKey, iPos, helper)
  270.     Else
  271.         f = BSearchArray(aStrs(), vKey, iPos, helper)
  272.     End If
  273.     If f Then
  274.         lblOut.Caption = "Found at position: " & iPos
  275.     Else
  276.         lblOut.Caption = "Insert at position: " & iPos
  277.     End If
  278.  End Sub
  279.  
  280. Private Sub cmdFindCollect_Click()
  281.     Dim iPos As Long
  282.     If BSearchCollection(nStrs, InputBox("Collection item to find: "), iPos, helper) Then
  283.         lblOut.Caption = "Found at position: " & iPos
  284.     Else
  285.         lblOut.Caption = "Insert at position: " & iPos
  286.     End If
  287. End Sub
  288.  
  289. Private Sub cmdFindSList_Click()
  290.     Dim v As Variant
  291.     v = InputBox("List item to find: ")
  292.     On Error Resume Next
  293.     list.Current = v
  294.     If Err Then lblOut.Caption = Err.Description
  295. End Sub
  296.  
  297. Private Sub cmdInsertCollect_Click()
  298.     Dim v As Variant, iPos As Long
  299.     v = InputBox("Collection item to insert: ")
  300.     If BSearchCollection(nStrs, v, iPos, helper) Then
  301.         lblOut.Caption = "Can't insert duplicate item: " & v
  302.     Else
  303.         lblOut.Caption = sEmpty
  304.         On Error GoTo IndexError
  305.         nStrs.Add v, , iPos
  306.         ShowCollect
  307.     End If
  308.     
  309.     Exit Sub
  310. IndexError:
  311.     ' Item needs to be inserted at end of collection
  312.     nStrs.Add v
  313.     ShowCollect
  314. End Sub
  315.  
  316. Private Sub cmdInsertSList_Click()
  317.     Dim s As String, iPos As Long
  318.     s = InputBox("List item to insert: ")
  319.     On Error Resume Next
  320.     list.Add s
  321.     If Err Then lblOut.Caption = Err.Description
  322. End Sub
  323.  
  324. Private Sub cmdRemoveCollect_Click()
  325.     Dim v As Variant, iPos As Long
  326.     v = InputBox("Collection item to remove: ")
  327.     If IsNumeric(v) Then
  328.         iPos = Val(v)
  329.         If iPos > nStrs.Count Or iPos < 0 Then
  330.             lblOut.Caption = "Invalid index: " & iPos
  331.             Exit Sub
  332.         End If
  333.     ElseIf BSearchCollection(nStrs, v, iPos, helper) Then
  334.         lblOut.Caption = sEmpty
  335.     Else
  336.         lblOut.Caption = "Item not in collection: " & v
  337.         Exit Sub
  338.     End If
  339.     nStrs.Remove iPos
  340.     ShowCollect
  341. End Sub
  342.  
  343. Private Sub cmdRemoveSList_Click()
  344.     Dim v As Variant, iPos As Long
  345.     v = InputBox("List item to remove: ")
  346.     On Error Resume Next
  347.     list.Remove v
  348.     If Err Then lblOut.Caption = Err.Description
  349. End Sub
  350.  
  351. Private Sub cmdReplaceSList_Click()
  352.     Dim vGet As Variant, vPut As Variant, iPos As Long
  353.     vGet = InputBox("List item to replace: ")
  354.     vPut = InputBox("New List item: ")
  355.     On Error Resume Next
  356.     list(vGet) = vPut
  357.     If Err Then lblOut.Caption = Err.Description
  358. End Sub
  359.  
  360. Sub chkDirection_Click()
  361.     helper.HiToLo = (chkDirection.Value = vbChecked)
  362.     list.HiToLo = (chkDirection.Value = vbChecked)
  363.     If esmlMode = esmlShuffle Or esmlMode = esmlUnsorted Then Exit Sub
  364.     SortAll
  365. End Sub
  366.  
  367. Private Sub lstSort_Click()
  368.     esmlMode = lstSort.ListIndex
  369.     helper.SortMode = esmlMode
  370.     SortAll
  371. End Sub
  372.  
  373. Sub ShowArray()
  374.     Dim i As Integer, s As String
  375.     Static fInitialized As Boolean
  376.     If esmlMode = esmlUnsorted Then
  377.         If fInitialized = False Then
  378.             For i = 1 To 10
  379.                 s = s & aConst(aInts(i)) & sCrLf
  380.             Next
  381.             fInitialized = True
  382.         Else
  383.             Exit Sub
  384.         End If
  385.     ElseIf esmlMode = esmlSortVal Then
  386.         For i = 1 To 10
  387.             s = s & aConst(aInts(i)) & sCrLf
  388.         Next
  389.     Else
  390.         For i = 1 To 10
  391.             s = s & aStrs(i) & sCrLf
  392.         Next
  393.     End If
  394.     txtArray.Text = s
  395. End Sub
  396.  
  397. Sub ShowCollect()
  398.     Dim i As Integer, s As String, v As Variant
  399.     s = sEmpty
  400.     For Each v In nStrs
  401.         s = s & v & sCrLf
  402.     Next
  403.     txtCollect.Text = s
  404. End Sub
  405.  
  406. Sub SortAll()
  407.     Select Case esmlMode
  408.     Case esmlUnsorted
  409.         ' Exit Sub
  410.     Case esmlSortVal
  411.         SortArray aInts(), , , helper
  412.         SortCollection nStrs, , , helper
  413.     Case esmlShuffle
  414.         ShuffleArray aStrs(), helper
  415.         ShuffleCollection nStrs, helper
  416.     Case Else
  417.         SortArray aStrs(), , , helper
  418.         SortCollection nStrs, , , helper
  419.     End Select
  420.     ShowArray
  421.     ShowCollect
  422.     list.SortMode = esmlMode
  423. End Sub
  424.  
  425. Function LookupString(A() As String, vKey As Variant) As Integer
  426.     Dim i As Integer
  427.     For i = 1 To 10
  428.         If A(i) = vKey Then
  429.             LookupString = i
  430.             Exit Function
  431.         End If
  432.     Next
  433.     LookupString = -1    ' Not found
  434. End Function
  435.  
  436. ' Uncomment to do tests when right click on Exit button
  437. #Const fTestListPlus = 1
  438.  
  439. #If fTestListPlus Then
  440. Private Sub cmdExit_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
  441.     ' Right click
  442.     If Button = 2 Then TestList
  443. End Sub
  444.  
  445. ' Test of some features
  446. Private Sub TestList()
  447.     Dim s As String, i As Long
  448.     Stop
  449.     Debug.Print list(20)
  450.     Debug.Print list(3)
  451.     Debug.Print list("Lion")
  452.     On Error Resume Next
  453.     i = list("Giraffe")
  454.     Debug.Print IIf(Err, "No Giraffe", "Giraffe")
  455.     
  456.     list(3) = "Deer"
  457.     list("Lion") = "Big Cat"
  458.     ShowAll
  459.     With list
  460.         Debug.Print .Item(20)
  461.         Debug.Print .Item(3)
  462.         Debug.Print .Item("Lion")
  463.         ShowAll
  464.         Debug.Print .Item("Giraffe")
  465.         .Item(3) = "Deer"
  466.         .Item("Lion") = "Big Cat"
  467.         
  468.         .Current = "dog"
  469.         .Current = 1
  470.         .Current = 4
  471.         Debug.Print .Current
  472.         Debug.Print .Item(.Current)
  473.         Debug.Print .IndexItem
  474.         Debug.Print .Text
  475.         .Add "Dog"
  476.         .Add "Tigger"
  477.         .Add "dog"
  478.         .Remove "dog"
  479.         .Remove "Marten"
  480.         .Remove 5
  481.         .Remove 20
  482.     End With
  483.     ShowAll
  484. End Sub
  485.  
  486. Private Sub ShowAll()
  487.     Dim v As Variant, s As String
  488.     For Each v In list
  489.         s = s & v & " "
  490.     Next
  491.     Debug.Print "List: " & s
  492. End Sub
  493. #End If
  494.  
  495.  
  496.